package club.cearnach.web.controller.admin;

import club.cearnach.web.controller.BaseController;
import club.cearnach.entity.MovieLanguage;
import club.cearnach.api.exception.MovieLanguageException;
import club.cearnach.api.form.PageBean;
import club.cearnach.api.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

/**
 * @author 阮胜
 * @date 2018/5/13 19:15
 */
@Api(tags = "后台电影语言控制类")
@Controller
@RequestMapping("/admin/movie/language")
@Slf4j
public class BackgroundMovieLanguageController extends BaseController {


    public BackgroundMovieLanguageController(IActorService actorService, IAdminService adminService, IDirectorService directorService, IMovieLanguageService movieLanguageService, IMovieService movieService, IMovieTypeService movieTypeService, IPersonService personService, IPlaceService placeService, IVisitorService visitorService) {
        super(actorService, adminService, directorService, movieLanguageService, movieService, movieTypeService, personService, placeService, visitorService);
    }

    @ApiOperation("电影语言列表")
    @GetMapping("/list")
    public String movieLanguageList(PageBean pageBean, Model model) {
        Page<MovieLanguage> movieLanguagePage = movieLanguageService.findAllOrderByCreateDate(pageBean);
        model.addAttribute(movieLanguagePage.getContent());
        model.addAttribute("totalPage", movieLanguagePage.getTotalPages());
        model.addAttribute("currentPage", pageBean.getPage());
        return "admin/movie-language-list";
    }

    @ApiOperation("保存电影语言")
    @PostMapping("/save")
    @ResponseBody
    public boolean saveMovieLanguage(MovieLanguage movieLanguage) throws MovieLanguageException {
        movieLanguageService.save(movieLanguage);
        return true;

    }

    @ApiOperation("检查电影语言是否已经存在")
    @GetMapping("/exist")
    @ResponseBody
    public boolean isExist(@RequestParam(defaultValue = "0") Integer id, String name) {
        return movieLanguageService.isExist(id, name);
    }

    @ApiOperation("跳转到编辑电影语言页面")
    @GetMapping("/edit/form/{id}")
    public String editForm(@PathVariable("id") Integer id, Model model) throws MovieLanguageException {
        MovieLanguage movieLanguage = movieLanguageService.findById(id);
        model.addAttribute(movieLanguage);
        return "admin/movie-language-save";
    }

    @ApiOperation("删除电影语言")
    @DeleteMapping("/delete/{id}")
    @ResponseBody
    public boolean delete(@PathVariable("id") Integer id) throws MovieLanguageException {
        movieLanguageService.delete(id);
        return true;
    }

    @ApiOperation("批量删除")
    @DeleteMapping("/delete/all")
    @ResponseBody
    public void deleteAll(@RequestParam(value = "id") Integer[] idArr) {
        movieLanguageService.deleteIn(idArr);
    }
}
